package com.example.jpa.repository;

import com.example.jpa.service.dto.RougeInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**
 * @author : zhayh
 * @Date : 2021-4-4 11:16
 * @Description:
 */

@Repository
public interface RougeRepository extends JpaRepository<RougeInfo, Long> {
    // 解析方法名创建查询
    Page<RougeInfo> findByRougeNameLikeOrBrandInfoBrandNameLikeOrShopInfoShopNameLike(String rougeName,
                                                                                      String brandName,
                                                                                      String shopName,
                                                                                      Pageable pageable);

    // JPQL 语句查询
    @Query("select a from RougeInfo a where a.rougeName like concat('%',?1,'%') " +
            "or a.brandInfo.brandName like concat('%',?1,'%') " +
            "or a.shopInfo.shopName like concat('%',?1,'%')")
    Page<RougeInfo> selectByKeyword(String keyword, Pageable pageable);

    // JPQL语句更新
    @Modifying(clearAutomatically = true)
    @Query("update RougeInfo set rougePrice=?2 where id=?1")
    int updateRougePrice(Long id, Float price);

}
